import java.sql.*;

public class TestTransaction {
	public static void main(String[] args) {
		
		Statement stat = null;
		Connection conn = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:lovemanai", "scott", "tiger");
			
			
			//关闭自动提交
			conn.setAutoCommit(false);
			
			stat = conn.createStatement();
			stat.addBatch("insert into dept2 values(50, 'GAME', 'XIAN')");
			stat.addBatch("insert into dept2 values(51, 'GAME', 'XIAN')");
			stat.addBatch("insert into dept2 values(52, 'GAME', 'XIAN')");
			stat.executeBatch();
			
			//手动提交
			conn.commit();
			
			//将提交模式设置回自动提交
			conn.setAutoCommit(true);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				if(conn != null) {
					conn.rollback();//回滚
					conn.setAutoCommit(true);
				}
			} catch(SQLException e1) {
				e1.printStackTrace();
			}
			
		} finally {
			try {
				if(stat != null) {
					stat.close();
					stat = null;
				}
				if(conn != null) {
					conn.close();
					conn = null;
				}
			} catch(SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
}
